package com.easy.util;


import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.transaction.annotation.Transactional;

import java.util.Properties;

public class EmailUtil {

        private static final String FROM ="3086153855@qq.com";
        private static final String  AUTO_CODE="jzleublvbizaddbe";
        private static final String  HOST="smtp.qq.com";
        private static final String  PORT="465";

        private static final Session session ;
        static{
            Properties props = new Properties();
            props.put("mail.smtp.host", HOST);
            props.put("mail.smtp.port", PORT);
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.ssl.enable", "true");
            session = Session.getInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(FROM, AUTO_CODE);
                }
            });
            //开启测试debug模式
            //session.setDebug(true);
        }

        public static void sendEmail(String to, String text) {
            Message message = new MimeMessage(session);
            try {
                message.addFrom(new Address[]{new InternetAddress(FROM)});
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                message.setSubject("easyA电竞酒店");
                message.setContent("<h1 style='color:red'>验证码:"+text+"</h1>", "text/html;charset=utf-8");
                //message.setText(text);
                Transport.send(message);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }

    public static void main(String[] args) {
        sendEmail("3086153855@qq.com","123456");
    }

    public static void sendNewPassword(String email, String newPassword) {
        Message message = new MimeMessage(session);
        try {
            message.addFrom(new Address[]{new InternetAddress(FROM)});
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            message.setSubject("easyA电竞酒店 - 您的新密码");
            message.setContent("<h1>您的新密码是: "+newPassword+"</h1><p style='color:red'>请尽快登录并修改密码</p>", "text/html;charset=utf-8");
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

